<?php

class hotSale extends Admin {

   private $tab = array( 'infoMan' => '信息管理','infoShow' => '信息展示');
   private $name = "热卖商品";
   private $salerType = array('全部','上衣', '卫衣/绒衫', '毛衣/针织衫', '羽绒服/棉服', '短袖T恤', '裙子', '吊带衫/背心',
                               '裤子',  '牛仔裤', '冬棉裤');
   private $source = array( '全部','阿里巴巴', '淘宝', '厂家' );
   private $age = array( '全部','0-3', '4-6', '7-12' );
   private $costPrice = array('全部', '10-20', '20-50', '50-80','80-100','100以上');
   private $addr = array( '全部','东莞','广州', '深圳', '湖州织里' );
   private $clothScore = array('1','2','3');
   private $style = array( '其他','运动','淑女', '休闲', '酷炫','卡通','图案', '韩派' );
   private $brand = array( '其他','巴拉巴拉','小猪班纳','笛莎', '巴布豆', '红孩儿','韩版' );

   public function index( $c, $a ) {
        $type = G( 'type', 'infoMan' );
        $method = "{$a}_{$type}";
        $this->$method( $c, $a, $type );
    }

    public function index_infoMan( $c, $a ) {
        $type = G( 'type', 'infoMan' );
        $this->table = strtolower( $c );

        $where = array( G( 'k' ) => G( 'v' ) );
        if ( $key = G( 'key' ) ) {
            $tmp = '';
            foreach ( $where as $k => $v ) {
                $tmp .= "{$k}='" . urldecode( $v ) . "' and ";
            }
            $where = $tmp ? ( $tmp . "title like '%{$key}%'" ) : '';
        }
        $result = $this->Paged()->findAll( $where );
        $pager = $result[ 'page' ];
        $result = $result[ 'result' ];

        //echo $this->dumpSql();
        include $this->display( $c );
    }

    public function index_infoShow( $c, $a ) {

        print_r("123");
        $type = G( 'type', 'infoShow' );
        include $this->display( $c );
    }

    public function search_hotSale( $c, $a ) {

        $type = G( 'type', 'infoMan' );
        $this->table = strtolower( $c );

        $where = array( G( 'k' ) => G( 'v' ) );
        if ( $key = G( 'key' ) ) {
            $tmp = '';
            foreach ( $where as $k => $v ) {
                $tmp .= "{$k}='" . urldecode( $v ) . "' and ";
            }
            $where = $tmp ? ( $tmp . "sn ={$key} " ) : '';
        }


        $result = $this->Paged()->findAll( $where );
        $pager = $result[ 'page' ];
        $result = $result[ 'result' ];


        include $this->display( $c ); // 加载模板

    }

    public function addClothEvaluate( $c, $a ) {
        $type = G( 'type', 'infoShow' );
        $this->table = strtolower( $c );

        $t=G('t');
        $where = "clothType = ".$t;

        $result = $this->find( $where); // 查找数据

        //echo $this->dumpSql();
        include $this->display( $c );
    }
    public function lookClothEvaluate( $c, $a ) {
        $type = G( 'type', 'infoShow' );
        $this->table =  "clothEvaluate";

        $t=G('sn');
        $where = "sn = ".$t;

        $result = $this->Paged()->findAll( $where );

        $pager = $result[ 'page' ];
        $result = $result[ 'result' ];

        //echo $this->dumpSql();
        include $this->display( $c );
    }

    public function search_mulCondition($c,$a)
    {
        $type = G( 'type', 'infoShow' );
        $m_salerType = G('salerType');
        $m_source = G('source');
        $m_age = G('age');
        $m_costPrice = G('costPrice');
        $m_addr = G('addr');

        if(array_search($m_salerType,$this->salerType))
        {
            $condition['clothType']=array_search($m_salerType,$this->salerType);
        }
        if(array_search($m_source,$this->source))
        {
            $condition['source']=array_search($m_source,$this->source);
        }
        if(array_search($m_age,$this->age))
        {
            $condition['age']=array_search($m_age,$this->age);
        }
        if(array_search($m_costPrice,$this->costPrice))
        {
            $condition['costPrice']=array_search($m_costPrice,$this->costPrice);
        }
        if(array_search($m_addr,$this->addr))
        {
            $condition['addr']=array_search($m_addr,$this->addr);
        }

        $this->table = strtolower( $c );




        $result = $this->Paged()->findAll( $condition );
        $pager = $result[ 'page' ];
        $result = $result[ 'result' ];

      include $this->display( $c );
    }

    public function saveClothEvaluate($c, $a)
    {
        $type = G( 'type', 'infoShow' );
        foreach ( $_POST as $key => $val ) {
            $rows[ $key ] = htmlspecialchars( trim( $val ) );
        }

        $rows[ 'dateline' ] = date("YmdHis");
         $rows[ 'user' ]  = $this->admin['author'];

        $sn = intval( G( "sn" ) );

        $this->table = "clothEvaluate"; // 数据库表名

        $sql = "xbl_clothEvaluate";
        $result = mysql_query("SELECT * FROM ".$sql);
        if(!$result)
        {
            $sql = "create TABLE `xbl_clothEvaluate` (
                `id` smallint(6) unsigned NOT NULL auto_increment,
                `sn` char(14) NOT NULL default '',
                `vote` smallint(5) unsigned NOT NULL default '0',
                `comments` varchar(100) NOT NULL DEFAULT '' COMMENT '评论',
                `user` varchar(100) NOT NULL DEFAULT '' COMMENT '用户',
                `dateline` date comment '评价时间',
                PRIMARY KEY  (`id`),
                KEY `dateline` (`dateline`)
                ) ENGINE=MyISAM AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;";
            $this->exec($sql);
        }

        $id = $this->create( $rows );
        $msg = $id ? 'success' : 'failed';

        exit( $msg );
    }

    public function edit( $c, $a ) {
        $type = G( 'type', 'infoMan' );
        if ( !$id = intval( G( 'id' ) ) ) {
            exit( '非法编辑商品' );
        }
        $this->table = strtolower( $c ); // 数据库表名
        $result = $this->find( array( 'id' => $id ) ); // 查找数据

        include $this->display( $c ); // 加载模板
    }

    public function add( $c, $a ) {
        $type = G( 'type', 'infoMan' );
        include $this->display( $c );
    }

    public function save( $c ) {
        # 获取表单数据
        foreach ( $_POST as $key => $val ) {
            $rows[ $key ] = htmlspecialchars( trim( $val ) );
        }

        switch($rows['price']/10)
        {
            case  0:
                $rows['costPrice'] = 0;
                break;
            case  1:
                $rows['costPrice'] = 1;
                break;
            case  2:
            case  3:
            case  4:
                $rows['costPrice'] =2;
                break;
            case  5:
            case  6:
            case  7:
                $rows['costPrice'] = 3;
                break;
            case  8:
            case  9:
                $rows['costPrice'] = 4;
                break;
            default:
               $rows['costPrice'] = 5;
               break;


        }


        $rows[ 'dateline' ] = strtotime( $rows[ 'dateline' ] );
        unset( $rows[ 'attach' ] );

        $attachId = explode(',',$_POST['attach']['id']);

        print_r($attachId);

        $rows[ 'attachments' ] = $this->getAttachTotal();
        for($k =0;$k < $rows[ 'attachments' ];$k++)
        {

            $sql = "select filepath from xbl_attachment where id = " .  $attachId[$k];
            $res = $this->findSql($sql);
            print_r($res);
            $rows["picAddr$k"] =$res[0]['filepath'];
        }
        # 注意 表单字段必须跟数据库表字段名一样
        $this->table = strtolower( $c ); // 数据库表名
        if ( $id = intval( G( "id" ) ) ) {
            $conditions = array( 'id' => $id );
            $msg = $this->update( $conditions, $rows ) ? 'success' : 'failed';
            $this->incrField( $conditions, 'attachments', $this->getAttachTotal() );
        } else {
            $id = $this->create( $rows );
            $msg = $id ? 'success' : 'failed';
        }

        $this->updateAttachment( $id, $c );

        exit( $msg );
    }

    # 删除商品
    public function del( $c ) {
        if ( !$id = intval( G( "id" ) ) ) {
            exit( '非法删除商品' );
        }

        #删除商品
        $this->table = strtolower( $c );
        $msg = $this->delete( array( "id" => $id ) ) ? 'success' : 'failed';

        # 删除附件
        $this->table = 'attachment';
        $this->delete( array( "aid" => $id, 'model' => strtolower( $c ) ) );
        exit( $msg );
    }

    public function get_date() {
        echo date( G( 'format', $this->setting[ 'timeformat' ] ) );
    }

    # 获取上传附件总数
    private function getAttachTotal() {
        $attach = explode( ",", $_POST[ 'attach' ][ 'id' ] );
        return $attach ? ( count( $attach ) - 1 ) : 0;
    }

    # 读取附件
    private function getAttachment( $id, $c ) {
        $this->table = "attachment";
        return $this->findAll( array( 'aid' => $id, 'model' => strtolower( $c ) ) );
    }

    # 更新附件
    private function updateAttachment( $id, $c ) {
        $attach = $_POST[ 'attach' ][ 'id' ];
        if ( !$attach ) {
            return false;
        }
        $attach = rtrim( $attach, "," );
        $this->table = "attachment";
        $this->update( "id in ({$attach})", array( 'aid' => $id, 'model' => strtolower( $c ) ) );
        return true;
    }
}

?>